package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.WmMdLayer;
import cn.stylefeng.guns.modular.biz.enums.WmMdLayerExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.WmMdLayerMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.WmMdLayerRequest;
import cn.stylefeng.guns.modular.biz.service.WmMdLayerService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;

/**
 * 煤场基础层高代码业务实现层
 *
 * @author 张朋飞
 * @since 2024/09/13 16:37
 */
@Service
public class WmMdLayerServiceImpl extends ServiceImpl<WmMdLayerMapper, WmMdLayer> implements WmMdLayerService {

	@Override
    public void add(WmMdLayerRequest wmMdLayerRequest) {
        WmMdLayer wmMdLayer = new WmMdLayer();
        BeanUtil.copyProperties(wmMdLayerRequest, wmMdLayer);
        this.save(wmMdLayer);
    }

    @Override
    public void del(WmMdLayerRequest wmMdLayerRequest) {
        WmMdLayer wmMdLayer = this.queryWmMdLayer(wmMdLayerRequest);
        this.removeById(wmMdLayer.getLayerId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(WmMdLayerRequest wmMdLayerRequest) {
        this.removeByIds(wmMdLayerRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(WmMdLayerRequest wmMdLayerRequest) {
        WmMdLayer wmMdLayer = this.queryWmMdLayer(wmMdLayerRequest);
        BeanUtil.copyProperties(wmMdLayerRequest, wmMdLayer);
        this.updateById(wmMdLayer);
    }

    @Override
    public WmMdLayer detail(WmMdLayerRequest wmMdLayerRequest) {
        return this.queryWmMdLayer(wmMdLayerRequest);
    }

    @Override
    public PageResult<WmMdLayer> findPage(WmMdLayerRequest wmMdLayerRequest) {
        LambdaQueryWrapper<WmMdLayer> wrapper = createWrapper(wmMdLayerRequest);
        Page<WmMdLayer> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<WmMdLayer> findList(WmMdLayerRequest wmMdLayerRequest) {
        LambdaQueryWrapper<WmMdLayer> wrapper = this.createWrapper(wmMdLayerRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private WmMdLayer queryWmMdLayer(WmMdLayerRequest wmMdLayerRequest) {
        WmMdLayer wmMdLayer = this.getById(wmMdLayerRequest.getLayerId());
        if (ObjectUtil.isEmpty(wmMdLayer)) {
            throw new ServiceException(WmMdLayerExceptionEnum.WM_MD_LAYER_NOT_EXISTED);
        }
        return wmMdLayer;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private LambdaQueryWrapper<WmMdLayer> createWrapper(WmMdLayerRequest wmMdLayerRequest) {
        LambdaQueryWrapper<WmMdLayer> queryWrapper = new LambdaQueryWrapper<>();

        Long layerId = wmMdLayerRequest.getLayerId();
        queryWrapper.eq(ObjectUtil.isNotNull(layerId), WmMdLayer::getLayerId, layerId);

        String layerName = wmMdLayerRequest.getLayerName();
        queryWrapper.like(ObjectUtil.isNotEmpty(layerName), WmMdLayer::getLayerName, layerName);

        String layerCode = wmMdLayerRequest.getLayerCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(layerCode), WmMdLayer::getLayerCode, layerCode);

        String remark = wmMdLayerRequest.getRemark();
        queryWrapper.like(ObjectUtil.isNotEmpty(remark), WmMdLayer::getRemark, remark);


        return queryWrapper;
    }

}
